package vn.vnvas.xoso.trungnam;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.vnvas.xoso.LayoutItem;
import vn.vnvas.xoso.Mien;
import vn.vnvas.xoso.R;
import vn.vnvas.xoso.myAdapter;
import vn.vnvas.xoso.utils.WebService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class miennam extends Activity {

	private ListView mListView;
	private ArrayList<Mien> mArrayList;
	private myAdapter adapter;

	private JSONArray jArray;
	private JSONObject jObject;
	private WebService mWebService;

	private ProgressDialog progressDialog;
	private String loc_id, tentinh, jString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.miennam);

		mListView = (ListView) findViewById(R.id.listViewMienNam);
		mArrayList = new ArrayList<Mien>();
		mWebService = new WebService();

		new Loading().execute();

	}

	private class Loading extends AsyncTask<String, Void, Object> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(miennam.this, "", "Loading..");
		}

		@Override
		protected Object doInBackground(String... arg0) {

			jString = mWebService.WsXoSo("TinhQuayThuong", "mien", "NAM");

			try {
				jArray = new JSONArray(jString);
				if (jArray.length() > 0) {
					for (int i = 0; i < jArray.length(); i++) {
						jObject = jArray.getJSONObject(i);

						tentinh = jObject.getString("LocName");
						loc_id = jObject.getString("LocId");

						Mien tinh = new Mien(tentinh,
								BitmapFactory.decodeResource(getResources(),
										R.drawable.nam), loc_id);
						mArrayList.add(tinh);

					}
				}
			} catch (Exception e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			adapter = new myAdapter(miennam.this, R.layout.layout_item,
					mArrayList);
			mListView.setAdapter(adapter);

			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int postition, long id) {
					String tinh_id = ((LayoutItem) view).loc_id.getText()
							.toString();
					Intent intent1 = new Intent(getBaseContext(),
							KetQuaTheoTinh_TrungNam.class);
					intent1.putExtra("tinh_id", tinh_id);
					startActivity(intent1);

				}
			});

		}

	}

}
